package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.DtsGrouponRulesMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGrouponRulesService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DtsGrouponRulesServiceImpl implements DtsGrouponRulesService {

    @Autowired
    DtsGrouponRulesMapper grouponRulesMapper;

    @Override
    public Map<String, Object> selectList(Integer page, Integer limit, String sort, Integer integer, String order) {
        IPage<DtsGrouponRules> iPage = new Page<>(page, limit);
        QueryWrapper<DtsGrouponRules> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(order) && StringUtils.isEmpty(sort)) {
            if (!"desc".equals(order)) {
                wrapper.orderByDesc(sort);
            } else {
                wrapper.orderByAsc(sort);
            }
        }
        if (integer != null) {
            wrapper.eq("goods_id", integer);
        }
        grouponRulesMapper.selectPage(iPage, wrapper);

        long total = iPage.getTotal();
        List<DtsGrouponRules> records = iPage.getRecords();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return data;
    }

    @Autowired
    DtsGoodsMapper mapper;

    @Override
    public DtsGrouponRules insret(DtsGrouponRules rules) {
        rules.setAddTime(new Date());
        rules.setUpdateTime(new Date());
        rules.setExpireTime(rules.getAddTime());
        rules.setDiscountMember(rules.getDiscountMember());
        rules.setGoodsId(rules.getGoodsId());

        rules.setDeleted(false);
        DtsGoods goods = mapper.selectById(rules.getGoodsId());
        if (goods != null) {
            rules.setGoodsName(goods.getName());
            rules.setPicUrl(goods.getPicUrl());
        } else {
            rules.setGoodsName(rules.getGoodsName());
            rules.setPicUrl(rules.getPicUrl());
        }

        grouponRulesMapper.insert(rules);
        return rules;
    }

    @Override
    public DtsGrouponRules update(DtsGrouponRules rules) {
        DtsGoods goods = mapper.selectById(rules.getGoodsId());
        rules.setAddTime(new Date());
        rules.setGoodsId(rules.getGoodsId());
        rules.setExpireTime(rules.getExpireTime());
        rules.setUpdateTime(new Date());
        rules.setDiscount(rules.getDiscount());
        rules.setDiscountMember(rules.getDiscountMember());
        if (goods != null) {
            rules.setGoodsName(goods.getName());
            rules.setPicUrl(goods.getPicUrl());
        } else {
            rules.setGoodsName(rules.getGoodsName());
            rules.setPicUrl(rules.getPicUrl());
        }
        grouponRulesMapper.updateById(rules);
        return rules;
    }

    @Override
    public void delete(DtsGrouponRules rules) {

        grouponRulesMapper.deleteById(rules.getId());
    }
}
